<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends Controller
{
	function Admin()
	{
		parent::Controller();	
		$this->load->model('user_model');
		if (empty($this->session->userdata['language']) || $this->session->userdata['language']=='vietnamese')
		{
			//echo "VN";
			$this->lang->load('vietnamese', 'vietnamese');
		}
		else
		{
			//echo "EN";
			$this->lang->load('english', 'english');
		}
		
	}

	function index()
	{
	    if ($this->session->userdata('logged_in') == TRUE)
	    {
			redirect('/adminpage/cpanel/');
	    }
		else
		{
			$this->load->view('admin/login');
		}
	}
	function login()
	{
	    $username = $this->input->xss_clean($this->input->post('username'));    
	    $password = md5($this->input->post('password').'dkw');
		$result=$this->user_model->check_login($username,$password);
		if($result->num_rows()>0)
		{
	        
			foreach ($result->result() as $row)
			{
				$data = array(
					   'username'  => $username,
					   'password'  => $password,
					   'user_ID'   => $row->ua_user_id,
					   'language'  => $this->input->post('language'),
					   'logged_in' => TRUE
					);
			}
                $this->session->set_userdata($data);
                redirect('/adminpage/cpanel/');
				//echo $data['user_ID'];
		}
		else
		{
	        $this->session->set_flashdata('message', '<div id="message">Oopsie, Username or password is incorrect</div>');
	        redirect('/admin/');

		}
    }
	function checkpass($pass='')
	{	
		if ($pass!='')
		{
			$result=$this->session->userdata('password')<>md5($pass.'dkw')?0:1;
			if ($result)
			{
				echo "<img src='".base_url()."/images/checked.gif' align='absmiddle'/> ".$this->lang->line('f_right_password').'<input type="hidden" id="correctpass" value=1 />';
			}
			else
			{
				echo "<img src='".base_url()."/images/unchecked.gif' align='absmiddle'/> ".$this->lang->line('f_wrong_password').'<input type="hidden" id="correctpass" value=0 />';
			}
		}
	}

	function logout()
	{
	    //$this->session->sess_destroy();
		$this->session->unset_userdata('logged_in');
	    redirect('/admin/');
	}


}
?>